<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2019/1/3
 * Time: 15:46
 */

namespace app\modules\specification\controller;

use app\modules\specification\AndSpecification;
use app\modules\specification\Item;
use app\modules\specification\NotSpecification;
use app\modules\specification\OrSpecification;
use app\modules\specification\PriceSpecification;
use yii\web\Controller;

class DefaultController extends Controller
{
    /**
     * 规格模式示例
     * 构建一个清晰的业务规则规范，其中每条规则都能被针对性地检查。
     * 每个规范类中都有一个称为isSatisfiedBy的方法，方法判断给定的规则是否满足规范从而返回 true 或 false。
     *
     * @date   2019/1/9
     * @author edmmss
     * @return bool
     */
    public function actionIndex()
    {
        $specification1 = new PriceSpecification(1, 20);
        $specification2 = new PriceSpecification(1, 30);

        $orSpecification = new OrSpecification($specification1, $specification2);

        $result = $orSpecification->isSatisfiedBy(new Item(10));
        var_dump($result);

        $andSpecification = new AndSpecification($specification1, $specification2);
        $result = $andSpecification->isSatisfiedBy(new Item(25));
        var_dump($result);

        $notSpecification = new NotSpecification($specification1);
        $result = $notSpecification->isSatisfiedBy(new Item(50));
        var_dump($result);

        return true;
    }
}